﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using OpenLightGroup.Common;
using OpenLightGroup.Common.ApplicationServices;
using OpenLightGroup.Common.ApplicationServices.HealthReportingProxy;
using OpenLightGroup.Common.Mvvm;

namespace Examples.ViewModels
{
    public class DashboardViewModel : OpenLightGroup.Common.Mvvm.BaseViewModel
    {
        private HealthReportingAgent EventAgent;
        private ObservableCollection<HealthReportingEntryMetaData> _eventList;
        private ICommand _showExceptionsCommand;
        private ICommand _showEventsCommand;
        private HealthReportingEntry _selectedEventDetails;
        private HealthReportingEntryMetaData _selectedEventMetadata;
        private HealthReportingServiceClient _client = new HealthReportingServiceClient();

        public DashboardViewModel()
        {
            this.EventAgent = new HealthReportingAgent();
            this._eventList = new ObservableCollection<HealthReportingEntryMetaData>();
            this._client.GetEventDetailsCompleted += new EventHandler<GetEventDetailsCompletedEventArgs>(Client_GetEventDetailsCompleted);

            this.EventAgent.StartPolling();
        }

        private void ShowEvents(object arg)
        {
            this._eventList.Clear();
            this._eventList.AddRange(this.EventAgent.LatestEvents);
            base.RaisePropertyChanged("EventList");
        }

        private void ShowExceptions(object arg)
        {
            this._eventList.Clear();
            this._eventList.AddRange(this.EventAgent.LatestExceptions);
            base.RaisePropertyChanged("EventList");
        }

        public ICommand ShowEventsCommand
        {
            get
            {
                if (this._showEventsCommand == null)
                {
                    this._showEventsCommand = new DelegateCommand(ShowEvents);
                }
                return this._showEventsCommand;
            }
        }

        public ICommand ShowExceptionsCommand
        {
            get
            {
                if (this._showExceptionsCommand == null)
                {
                    this._showExceptionsCommand = new DelegateCommand(ShowExceptions);
                }
                return this._showExceptionsCommand;
            }
        }

        public HealthReportingEntryMetaData SelectedEventMetadata
        {
            get
            {
                return this._selectedEventMetadata;
            }
            set
            {
                if (this._selectedEventMetadata != value)
                {
                    this._selectedEventMetadata = value;
                    base.RaisePropertyChanged("SelectedEventMetadata");
                    this._client.GetEventDetailsAsync(this._selectedEventMetadata.EventId);
                }
            }
        }

        void Client_GetEventDetailsCompleted(object sender, GetEventDetailsCompletedEventArgs e)
        {
            this._selectedEventDetails = e.Result;
            base.RaisePropertyChanged("SelectedEventDetails");
        }

        public ObservableCollection<HealthReportingEntryMetaData> EventList
        {
            get
            {
                return this.EventAgent.LatestEvents;
            }
            set
            {
                if (this._eventList != value)
                {
                    this._eventList = value;
                    base.RaisePropertyChanged("EventList");
                }
            }
        }

        public HealthReportingEntry SelectedEventDetails
        {
            get
            {
                return this._selectedEventDetails;
            }
            set
            {
                if (this._selectedEventDetails != value)
                {
                    this._selectedEventDetails = value;
                    base.RaisePropertyChanged("SelectedEventDetails");
                }
            }
        }
        
    }
}